package id.mankel.commons.crypto.encoding;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * @author Mankel
 */
public class Encoder {

    private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    public static String toHexString(byte[] data) {
        int length = data.length;
        char[] chars = new char[length << 1];
        for (int i = 0, j = 0; i < length; i++) {
            chars[j++] = HEX_DIGITS[(0xF0 & data[i]) >>> 4];
            chars[j++] = HEX_DIGITS[0x0F & data[i]];
        }
        return new String(chars);
    }

    public static String toBase64String(String s) {
        return Base64.getEncoder().encodeToString(s.getBytes(StandardCharsets.UTF_8));
    }

}
